package online.heycm.platform.common.entity.exception;

import online.heycm.platform.common.entity.result.ErrorCode;

/**
 * 业务异常
 *
 * @author heycm
 * @since 2023/11/19 18:25
 */
public class BizException extends RuntimeException {

    private int code;

    public BizException(String message) {
        super(message);
    }

    public BizException(int code, String message) {
        super(message);
        this.code = code;
    }

    public BizException(ErrorCode errorCode) {
        super(errorCode.message());
        this.code = errorCode.code();
    }

    public int getCode() {
        return code;
    }

    @Override
    public String toString() {
        return "BizException{" +
                "code=" + getCode() +
                ", message=" + getMessage() +
                '}';
    }

    public static void biz(String message) {
        throw new BizException(message);
    }

    public static void biz(int code, String message) {
        throw new BizException(code, message);
    }

    public static void biz(ErrorCode errorCode) {
        throw new BizException(errorCode);
    }

    public static void runtime(String message) {
        throw new RuntimeException(message);
    }
}
